<?php
define('DDPPATH',dirname(dirname(__FILE__)).'/');

//下拉表单 $array:数组  $name:select的name和id  $id:被选中选项的id  $interval:颜色间隔
function select($array,$name,$id=10000,$interval=5){
	$i=0;
	echo "<select name='$name' id='$name'>";
	foreach($array as $key=>$val){
		if($id==$key) $select='selected';
		else $select='';
		if($i!=0){
			if((floor(($i-1)/$interval))%2!=0) $bg="#dfefd7";
		    else $bg="";
		}
		echo "<option value='$key' $select style='background:$bg'>$val</option>";
		$i++;
	}
	echo "</select>";
}

function sql_sel($table,$sel_field,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;
}

function sql_sel_arr($table,$sel_field,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$arr=array();
	$sql="select $sel_field from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			$arr[]=$row;
		}
	}
	return $arr;
}

function one2two($arr){//一维数组转二维数组
    if(count($arr, 1) == count($arr)){
	    $arr=array($arr);
    }
    return $arr;
}

function sql_count($table,$count_field,$wh_field,$wh_value,$and=''){//and为自定义的筛选条件 例：$and='and id=1'
	global $BIAOTOU;
	$sql="select count($count_field) from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query = mysql_query($sql);
	$row=mysql_fetch_array($query);
	return $row[0]?$row[0]:0;
}

function sql_sum($table,$count_field,$wh_field,$wh_value,$and=''){//and为自定义的筛选条件 例：$and='and id=1'
	global $BIAOTOU;
	$sql="select sum($count_field) from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	$query = mysql_query($sql);
	$row=mysql_fetch_array($query);
	return $row[0]?$row[0]:0;
}

function sql_update_one($table, $up_field, $up_value, $wh_field, $wh_value, $xs = 0) { //更新某个字段的值，$xs表示更新的方式，0表示累加，1表示更新，2表示减
	global $BIAOTOU;
	if ($xs == 0){
		$sql = "update " . $BIAOTOU . "$table set $up_field=$up_field+'$up_value' where $wh_field='$wh_value'";
	}
	if ($xs == 1){
		$sql = "update " . $BIAOTOU . "$table set $up_field='$up_value' where $wh_field='$wh_value'";
	}
	if ($xs == 2){
		$sql = "update " . $BIAOTOU . "$table set $up_field=$up_field-'$up_value' where $wh_field='$wh_value'";
	}
	$query = mysql_query($sql);
	if ($query){
		return sql_sel($table, $up_field, $wh_field, $wh_value);
	}
	else{
		return false;
	}
}

function sql_insert_one($table,$filed_arr){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($filed_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($query) return 1;
	else return mysql_error();
}

function sql_update($table,$field_arr,$wh_field,$wh_value,$xs=0){ //0表示更新 1表示同加  2表示同减
    global $BIAOTOU;
	$sql="";
	foreach($field_arr as $k=>$v){
		if($xs==0){
		    $sql="`$k`='$v',".$sql;
		}
		elseif($xs==1){

		    $sql="`$k`=`$k`+'$v',".$sql;
		}
		elseif($xs==2){
		    $sql="`$k`=`$k`-'$v',".$sql;
		}
	}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where `$wh_field`=$wh_value;";
	mysql_query($sql);
}

function count_sql($table,$count_field,$where){//and为自定义的筛选条件 例：$and='and id=1'
	global $BIAOTOU;
	$sql="select count($count_field) from $BIAOTOU$table where $where";
	$query = mysql_query($sql);
	$row=mysql_fetch_array($query);
	return $row[0]?$row[0]:0;
}

/////////////////////////////////////////////////////////////////////////
function update_sql($table,$field_arr,$where,$xs,$alert=0){ //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
    global $BIAOTOU;
	$sql="";
	foreach($field_arr as $k=>$v){
		if($xs==0){
		    $sql="`$k`='$v',".$sql;
		}
		elseif($xs==1){
		    $sql="`$k`=`$k`+'$v',".$sql;
		}
		elseif($xs==2){
		    $sql="`$k`=`$k`-'$v',".$sql;
		}
	}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where $where;";
	if($alert==0){
		mysql_query($sql);
	}
	elseif($alert==1){
		echo $sql;
	}
	elseif($alert==2){
		mysql_query($sql);
	    echo $sql; 
	}
}

function insert_one_sql($table,$filed_arr,$alert=0){ 
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($filed_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return mysql_insert_id();
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return mysql_insert_id();
	    else return mysql_error();
	}
	
}

function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

function sel_arr_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$arr=array();
	$sql="select $sel_field from $BIAOTOU$table where $where ";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			$arr[]=$row;
		}
	}
	return $arr;
}

function delete_sql($table,$where){
	global $BIAOTOU;
	$sql="delete from $BIAOTOU$table where $where";
	mysql_query($sql);
}

//////////////////////////////////////////////////////////////////
function sql_delete($table,$wh_field,$wh_value,$and=""){
	global $BIAOTOU;
	$sql="delete from $BIAOTOU$table where $wh_field='$wh_value' ".$and;
	mysql_query($sql);
}

function sqltoarr($sql){
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){
        $arr[]=$row;
    }
	return $arr;
}


function arr_diff($arr1,$arr,$xs=1){ //xs=0，arr是键值  xs=1，arr是键名
	if($xs==1){
		foreach($arr as $k=>$v){
			$arr1[$v]='3645dfgsdf78ufasdgasdt345rsdfsdf';
			$arr[$v]='3645dfgsdf78ufasdgasdt345rsdfsdf';
		}
		$arr = array_diff($arr1, array('3645dfgsdf78ufasdgasdt345rsdfsdf'));
	}
	else{
		$arr2=$arr;
		$arr = array_diff($arr1, $arr2);
	}
    return $arr;
}

function arr_add($arr,$key){
	$arr[]=$key;
	return $arr;
}

function arr_edi($arr,$key,$value,$xs=1){ //xs=0，key是键值  xs=1，key是键名
    if($xs==0) $key=array_search($key,$arr);
    $arr[$key]=$value;
    return $arr;
}

function array_remove_empty($arr, $trim = true){ //数组去空
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            array_remove_empty($arr[$key]);
        } else {
            $value = trim($value);
            if ($value == '') {
                unset($arr[$key]);
            } elseif ($trim) {
                $arr[$key] = $value;
            }
        }
    }
	return $arr;
}

function empty2zero($arr,$keyarr){ //指定键值空转0
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            empty2zero($arr[$key]);
        } else {
            $value = trim($value);
            if ($value == '' and in_array($key,$keyarr)) {
                $arr[$key] = 0;
            }
        }
    }
	return $arr;
}

function check_content($arr){ //数组加addslashes
	foreach ($arr as $key => $value) {
        if (is_array($value)) {
            check_content($arr[$key]);
        } else {
            $value = trim($value);
            if(get_magic_quotes_gpc()==0){
		        $arr[$key] = addslashes($value);
	        }
        }
    }
	return $arr;
}

function detach_arr($arr, $name) {  //将二维数组按照要求，把相应的字段抽出来
	foreach ($arr as $key => $val) {
		$arr[$key] = $val[$name];
	}
	return $arr;
}

function get_object_vars_final($obj) {
	if (is_object($obj)) {
		$obj = get_object_vars($obj);
	}

	if (is_array($obj)) {
		$n=0;
		foreach ($obj as $key => $value) {
			if(($key=='bill_yyyymmdd' or $key=='u_id') and $n>0){
				if(is_object($value)) $value='';
			}
			$obj[$key] = get_object_vars_final($value);

			$n++;
		}
	}
	return $obj;
}

function alert($word,$url=''){
	if(is_numeric($url)){
	    echo "<script>alert('".$word."');history.go(".$url.");</script>";
	}
	if($url==''){
		echo "<script>alert('".$word."');history.go(-1);</script>";
	}
	else{
	    echo "<script>alert('".$word."');window.location='".$url."';</script>";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function re_login($name,$pwd,$dduserid,$savelogin=""){
    $_SESSION['duoduouser'] = $name;
    $_SESSION["dduserid"] = $dduserid;
	if(strlen($pwd)!=32){
	    $pwd=md5($pwd);
	}
	if($savelogin){
	    setcookie("ddusername", $name, time() + 1000 * 24 * 60 * 60);
        setcookie("ddpassword", $pwd, time() + 1000 * 24 * 60 * 60);
	}
}

function zsjf($ddusername,$trade_id,$fxje){
    global $set;
	if($set['gwsjf']>0 and $ddusername!='' and $trade_id!='' and $fxje>0){ //淘宝购物同时送积分
	    $jifen=round($fxje*$set['gwsjf']);
		//给该会员加积分
		$field_arr=array("jifen"=>$jifen);
		update_sql('user',$field_arr,"ddusername='$ddusername'",1,0);
		//添加明细
		$field_arr=array('ddusername'=>$ddusername,'shijian'=>'交易积分','je'=>$jifen,'addtime'=>date('Y-m-d H:i:s'),'memo'=>'交易号'.$trade_id);
	    sql_insert_one('jfmingxi',$field_arr);
		//添加站内信
		$field_arr=array('title'=>'您获得了新的交易积分','content'=>'您完成了一笔交易，获得了'.$jifen.'积分','addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$ddusername,'senduser'=>'网站客服');
		sql_insert_one('msg',$field_arr);
	}
}

function inc_fanli($dduserid,$fxje,$commission,$order_code){
    sql_update_one("user", "money", $fxje, "Id", $dduserid, 0);//增加会员金额
	$ddusername=sql_sel_one("user", "ddusername", "Id", $dduserid);//求会员名

	//用户消息
	$title='您获得了新的交易返现！';
	$content="交易号为：".$order_code."的交易确认完毕，您获得了 ".$fxje." 元的返现！";
	$filed_arr=array('title'=>$title,'content'=>$content,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$ddusername);
	sql_insert_one("msg",$filed_arr);

	//用户明细
	$shijian="交易返现";
	$memo="交易号$order_code";
	$filed_arr=array('ddusername'=>$ddusername,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$fxje,'memo'=>$memo);
	sql_insert_one("mingxi",$filed_arr);

	//求推荐人
	tgzs($ddusername,$order_code,$commission,$x='mall');
}

function inc_jifen($dduserid,$fxje,$commission,$order_code){
    sql_update_one("user", "jifen", $fxje, "Id", $dduserid, 0);//增加会员积分
	$ddusername=sql_sel_one("user", "ddusername", "Id", $dduserid);//求会员名

	//用户消息
	$title='您获得了新的交易积分！';
	$content="交易号为：".$order_code."的交易确认完毕，您获得了 ".$fxje." 积分！";
	$filed_arr=array('title'=>$title,'content'=>$content,'addtime'=>date('Y-m-d H:i:s'),'see'=>0,'ddusername'=>$ddusername);
	sql_insert_one("msg",$filed_arr);

	//用户明细
	$shijian="交易积分";
	$memo="交易号$order_code";
	$filed_arr=array('ddusername'=>$ddusername,'shijian'=>$shijian,'addtime'=>date('Y-m-d H:i:s'),'je'=>$fxje,'memo'=>$memo);
	sql_insert_one("mingxi",$filed_arr);

	//求推荐人
	tgzs($ddusername,$order_code,$commission,$x='mall');
}